# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Group Model"""

import datetime
from zoom import zoomer
import dzentity, dzmodel

class Group(dzmodel.Record):
    def clear(self):
            self.NAME  = self.DESCR = ''
            self.TYPE  = 'U'
            self.DTADD = self.DTUPD = datetime.datetime.now()

    def get_MEMBERS(self):
        d = zoomer.db('select * from dz_members where groupid=%s',self.GROUPID)
        return [item.USERID for item in d]

    def get_SUBGROUPS(self):
        d = zoomer.db('select * from dz_subgroups where groupid=%s',self.GROUPID)
        return [item.SUBGROUPID for item in d]

    def get_SUPERGROUPS(self):
        d = zoomer.db('select * from dz_subgroups where subgroupid=%s',self.GROUPID)
        return [item.GROUPID for item in d]

    def remove_member(self,userid):
        zoomer.db('delete from dz_members where groupid=%s and userid=%s',self.GROUPID,userid)

    def add_member(self,userid):
        zoomer.db('insert into dz_members (groupid,userid) values (%s,%s)',self.GROUPID,userid)

    def remove_subgroup(self,subgroupid):
        zoomer.db('delete from dz_subgroups where groupid=%s and subgroupid=%s',self.GROUPID,subgroupid)

    def add_subgroup(self,subgroupid):
        zoomer.db('insert into dz_subgroups (groupid,subgroupid) values (%s,%s)',self.GROUPID,subgroupid)

    def remove_supergroup(self,supergroupid):
        zoomer.db('delete from dz_subgroups where groupid=%s and subgroupid=%s',supergroupid,self.GROUPID)

    def add_supergroup(self,supergroupid):
        zoomer.db('insert into dz_subgroups (groupid,subgroupid) values (%s,%s)',supergroupid,self.GROUPID)

#    def add_member(self,userid):
#        zoomer.db('insert into dz_memebers

class Groups(dzmodel.Table):
    def __init__(self):
        dzmodel.table_prefix = 'dz_'
        dzmodel.Table.__init__(self,Group)
        self.table_key = 'GROUPID'

    def find_by_name(self,group_name):
        rec = zoomer.db('select groupid from dz_groups where name=%s',group_name)
        if rec:
            return self.seek(rec[0].GROUPID)

if __name__ == '__main__':
    import unittest

    class TestGroup(unittest.TestCase):
        def test_instantiate(self):
            groups = Groups()
            self.assert_(groups)

        def test_get_group(self):
            groups = Groups()
            group = groups.seek(2)
            self.assertEqual(group.GROUPID,2)
            self.assertEqual(group.NAME,'users')

        def test_new_group(self):
            group = Group()
            self.assertEqual(group.NAME,'')
            self.assertEqual(group.TYPE,'U')
            self.assertEqual(group.DESCR,'')

        def test_get_members(self):
            groups = Groups()
            group = groups.seek(1)
            members = group.MEMBERS
            assert len(members)>0

        def test_find_by_name(self):
            groups = Groups()
            group = groups.find_by_name('administrators')
            self.assertEqual(group.GROUPID,1)

    unittest.main()

